﻿ ' Automated Search & Replace routine for Visual Studio
    Public Sub MySearchReplace(ByVal FindWhat As String, ByVal ReplaceWith As String, ByVal WholeWord As Boolean, ByVal IsRegEx As Boolean)
        Try
            DTE.ExecuteCommand("Edit.Replace")
            DTE.Find.Action = vsFindAction.vsFindActionReplaceAll
            DTE.Find.FindWhat = FindWhat
            DTE.Find.ReplaceWith = ReplaceWith

            ' Search only the current document
            DTE.Find.Target = vsFindTarget.vsFindTargetCurrentDocument

            ' Search the entire Solution but be cautious of resources
            ' DTE.Find.Target = vsFindTarget.vsFindTargetSolution

            ' Search the current project
            ' DTE.Find.Target = vsFindTarget.vsFindTargetCurrentProject

            ' If you want to only update text that is selected, 
            ' use: vsFindTarget.vsFindTargetCurrentDocumentSelection()
            DTE.Find.MatchCase = True
            DTE.Find.MatchWholeWord = WholeWord
            DTE.Find.MatchInHiddenText = True

            If IsRegEx = True Then
                DTE.Find.PatternSyntax = vsFindPatternSyntax.vsFindPatternSyntaxRegExpr
            Else
                DTE.Find.PatternSyntax = vsFindPatternSyntax.vsFindPatternSyntaxLiteral
            End If

            DTE.Find.ResultsLocation = vsFindResultsLocation.vsFindResultsNone
            DTE.Find.Action = vsFindAction.vsFindActionReplaceAll
            DTE.Find.Execute()
        Catch ex As Exception
            MsgBox("Error on MySearchReplace." + vbLf + vbLf + "Message = " + ex.Message + "." + vbLf + vbLf + "FindWhat = " + FindWhat + vbLf + vbLf + "Stack Trace = " + ex.StackTrace, MsgBoxStyle.Critical, "Error")
        End Try
    End Sub
